package com.zhanghe.study.spring4.beans.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zh
 * @date 2021/1/6 11:57
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserService userService;

    @Transactional
    public void transer(int from,int to,double money){
        userDao.addAccount(to,money);
        double account = userDao.findAccountById(from);
        if(account < money){
            throw new RuntimeException("余额不足");
        }
        userDao.subtractAccount(from,money);
    }

    // public事务生效
    @Transactional
    public void save(int user,double money){
        userDao.addAccount(user,money);
        System.out.println("public的事务方法");
    }

    // 事务不生效
    @Transactional
    void save1(int user,double money){
        userDao.addAccount(user,money);
        System.out.println("default的事务方法");
    }

    // 事务不生效
    @Transactional
    protected void save2(int user,double money){
        userDao.addAccount(user,money);
        System.out.println("protected的事务方法");
    }


    // 事务不生效
    @Transactional
    private void save3(int user,double money){
        // userDao无法注入
        userDao.addAccount(user,money);
        System.out.println("private的事务方法");
    }

    @Transactional
    public void save5(int user,double money) throws Exception {
        userService.saveException(user, money);
        System.out.println("抛出Exception异常的事务方法");
    }

    @Transactional
    public void saveException(int user,double money) throws Exception {
        userDao.addAccount(user,money);
        throw new Exception("aaa");
    }

    @Transactional
    public void save6(int user,double money){
        userService.saveCatch(user, money);
        System.out.println("catch的事务方法");
    }

    @Transactional
    public void saveCatch(int user,double money){
        userDao.addAccount(user,money);
        try{
            int i = 1/0;
        } catch (Exception e){
            System.out.println("异常了");
        }
    }

    @Transactional
    public void testEntrance(int user,double money,int status){
        this.entrance(user, money, status);
    }


    // 测试事务失效
    public void entrance(int user,double money,int status){
        if(status == 0){ // 事务生效
            userService.save(user,money);
        } else if(status == 1){ // 事务不生效
            userService.save1(user,money);
        }else if(status == 2){ // 事务不生效
            userService.save2(user,money);
        } else if(status == 3){ // 事务不生效
            userService.save3(user,money);
        } else if(status == 4){ // 使用this调用，事务不生效
            this.save(user,money);
        } else if(status == 5){ // 抛出Exception,不会回滚
            try {
                userService.save5(user, money);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if(status == 6){ // 异常捕获了，正常入库，不会回滚
            this.save(user,money);
        }
    }


}
